package p.ithorns.sample.moudle2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import p.ithorns.sample.ModulePath;

import java.util.Set;

/**
 * ModuleServletConfigScanner
 * <p>
 * 模块路径类扫描器
 * 扫描所有实现了
 *
 * @author ithorns
 * @version 1.0.0
 * @date 2021/10/26 21:34
 */
public class ModuleServletConfigScanner extends ClassPathBeanDefinitionScanner {

    private final static Logger log = LoggerFactory.getLogger(ModuleServletConfigScanner.class);


    final BeanDefinitionRegistry registry;

    public ModuleServletConfigScanner(BeanDefinitionRegistry registry) {
        super(registry);
        this.registry = registry;

        // 设置为 false 以避免扫描枚举和接口等
        resetFilters(false);

        // 添加类型过滤，只扫描实现了 ModulePath 接口的类
        addIncludeFilter(new AnnotationTypeFilter(ModulePath.class));
    }


    public Set<BeanDefinition> scan(String packageName) {
        // 扫描整个类路径
        return findCandidateComponents(packageName);
    }


    @Override
    public boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        AnnotationMetadata metadata = beanDefinition.getMetadata();
        return metadata.isIndependent() && !metadata.isAnnotation() && (metadata.isAbstract() || metadata.isInterface());
    }

}